import axios from 'axios';

const API_BASE_URL = 'http://localhost:8001/data';
// '''
// 本文件用于封装API请求,主要讲后台分析好的数据进行展现。
// '''

// 获取市场涨跌分布数据
export const fetchMarketDistribution = async () => {
  try {
    const response = await axios.get(`${API_BASE_URL}/market/distribution`);
    
    return response.data;
  } catch (error) {
    console.error('Error fetching market distribution data:', error);
    throw error;
  }
};

// 获取近60个交易日的上涨/下跌家数数据
export const fetchMarketTrend = async () => {
  try {
    const response = await axios.get(`${API_BASE_URL}/market/trend/`, {
      params: {
        days: 60
      }
    });
    console.log('获取市场涨跌trend数据成功', response.data)
    return response.data;
  } catch (error) {
    console.error('Error fetching market trend data:', error);
    throw error;
  }
};
export const fetchMarketIndices = async () => {
  try {
    const response = await axios.get(`${API_BASE_URL}/market/indices/`);
    
    return response.data;
  } catch (error) {
    console.error('Error fetching market data:', error);
    throw error;
  }
};



export const saveStrategy = async (strategyData) => {
  try {
    const response = await axios.post(`${API_BASE_URL}/prices/`, strategyData);
    return response.data;
  } catch (error) {
    console.error('Error saving strategy:', error);
    throw error;
  }
};

export const fetchKLineData = async (tsCode = '000001.SH') => {
  try {
    const response = await axios.get(`${API_BASE_URL}/market/kline/daily/`, {
      params: {
        ts_code: tsCode,
      }
    });
    return response.data;
  } catch (error) {
    console.error('Error fetching K-line data:', error);
    throw error;
  }
};

export const fetchMinuteData = async (tsCode = '000001.SH') => {
  try {
    const response = await axios.get(`${API_BASE_URL}/market/tick/`, {
      params: {
        ts_code: tsCode
      }
    });
    return response.data;
  } catch (error) {
    console.error('Error fetching minute data:', error);
    throw error;
  }
};

export const fetchWeeklyKLineData = async (tsCode = '000001.SH') => {
  try {
    const response = await axios.get(`${API_BASE_URL}/market/kline/weekly/`, {
      params: {
        ts_code: tsCode
      }
    });
    return response.data;
  } catch (error) {
    console.error('Error fetching weekly K-line data:', error);
    throw error;
  }
};

export const fetchMonthlyKLineData = async (tsCode = '000001.SH') => {
  try {
    const response = await axios.get(`${API_BASE_URL}/market/kline/monthly/`, {
      params: {
        ts_code: tsCode
      }
    });
    return response.data;
  } catch (error) {
    console.error('Error fetching monthly K-line data:', error);
    throw error;
  }
};